/*
 * @Author: yangzonglong
 * @Date: 2020-09-10 10:52:18
 * @version: v1.0.0
 * @Descripttion: 通用message
 * @LastEditors: yangzonglong
 * @LastEditTime: 2021-08-25 09:14:14
 * @Auditor: 
 */
import { message } from 'antd';

type ErrorProps = {
  code?: string;
  message?: string
}

const DURATION = 3;
const top: any = window.top; // navbarx

let timeout: any = null;

export function success(msg?: string, duration?: number) {
  const _msg = window.$app.t(msg || 'success');
  if(top.success) return top.success(_msg);
  message.success(_msg, duration || DURATION)
}

export function error(msg?: string, err?: ErrorProps) {
  if(timeout) {
    clearTimeout(timeout);
    timeout = null;
  }
  
  timeout = setTimeout(() => {
    let _msg = window.$app.t(msg || 'error');

    // 子项目模式被嵌套在core，向上抛错
    if(top.error) return top.error(_msg, err);

    // 处理开发模式下报错
    if(_msg === msg && err && err.message) _msg = err.message;

    message.error(_msg, DURATION);

    clearTimeout(timeout);
    timeout = null;
  }, 200)
}

export function warning(msg: string, duration?: number) {
  if(top.warning) return top.warning(window.$app.t(msg));
  message.warning(window.$app.t(msg), duration || DURATION)
}

export default {
  success,
  error,
  warning
}